// pages/jugdge/jugdge.js
import {request} from '../../request/index.js'
import regeneratorRuntime, { async } from '../../lib/runtime/runtime';
import Dialog from '../../vant/dist/dialog/dialog'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 巡检记录
    JudgeForm:[],
    // 隐患详情
    DgeForm: [],
    // 巡检时间
    DuringTime: '',
    // 判断是否有巡检隐患
    IfJudge: true,
    IfJudge1: true,
    AllInfo: [],
    // 是否被选中
    show:false,
    // 供电所信息
    deptForm:[],
    // 隐患类型
    typeStyle:'',
    dgeInfo: '', // 隐患描述信息
    imgList: '', // 隐患图片数组
    chkMin: '',
    jiazai: false, //加载
  },


  onClose() {
    this.setData({ show: false });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getJudgeForm(false)
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
   /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    // qqmapsdk = new QQMapWX({
    //   key: 'TUHBZ-ZPFKU-XQAVA-BC4M4-ER4IZ-BXBLL'
    // });
    this.getJudgeForm(true);
    console.log(1)
  },
  // 获取大部分信息
async getJudgeForm(refresh){
  const res = await request({
    url: '/check/findByChecker/' + wx.getStorageSync('empId'),
    header:{
      'Authorization':'Bearer '+wx.getStorageSync('token'),
    },
    method: "get",
  })
  // console.log(res)
  if (res.data.status !== 200){
    var title = res.data.msg
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    })
    if (title === '您还未登录'){
      wx.reLaunch({
        url: '/pages/index/index',
      })
    }
  }
  else {
    if (! refresh) {
      wx.showLoading({
       title: '拼命加载中',
     })
   }
    var JudgeForm = res.data.data.reverse()
    console.log(JudgeForm)
    if (JudgeForm[0]) {
      var arr = []
      JudgeForm.forEach(function(item){
        var infos = (item.chkInfo+ "").split('imgGroup:')[0]
            var info = (infos === 'null') ? '暂无巡检信息' : infos
            var imgstr = (item.chkInfo+ "").split('imgGroup:')[1]
            var arr1 = []
            if(imgstr) {
                arr1 = (imgstr+ "").split(",")
            }
            else {
                arr1 = ["null"]
            }
            arr.push({item,info: info,img: arr1})
      })
      if (refresh){
        this.setData({
          JudgeForm: arr,
          IfJudge: false,
          jiazai: true
        },wx.stopPullDownRefresh())
      }
      else{
        this.setData({
          JudgeForm: arr,
          IfJudge: false,
          jiazai: true
        },wx.hideLoading())
      }
    }
    else{
      // 没有巡查记录
      if (refresh) {
        this.setData({
          IfJudge1: false
        },wx.stopPullDownRefresh())
      }
      else {
        this.setData({
          IfJudge1: false
        },wx.hideLoading())
      }
      
    }
  }
},

  // 查询隐患信息来获取隐患分类
  async getdgeTypeName(id){
    const res = await request({
      url: '/danger/findById/' + id,
      header:{
        'Authorization':'Bearer '+wx.getStorageSync('token'),
      },
      method: "get",
    })
    // console.log(res)
    if (res.data.status !== 200){
      var title = res.data.msg
      wx.showToast({
        title: title,
        icon: 'none',
        duration: 2000
      })
    }else{
      var dgeInfo = res.data.data
      
      if (dgeInfo) {
          var infos = (dgeInfo.dgeInfo+ "").split('imgGroup:')[0]
          var info = (infos === 'null') ? '暂无隐患信息' : infos
          var imgstr = (dgeInfo.dgeInfo+ "").split('imgGroup:')[1]
          var arr1 = []
          if(imgstr) {
              arr1 = (imgstr+ "").split(",")
          }
          else {
              arr1 = ["null"]
          }
          var type = dgeInfo.dgeLevel
          var classstyle = ''
          if (type == '一般' ) {
              classstyle = 'normal'
          } else if (type == '严重') {
            classstyle = 'serious'
          } else {
            classstyle = 'important'
          }
          this.setData({
            DgeForm: dgeInfo,
            typeStyle: classstyle,
            show: true,
            dgeInfo: info,
            imgList: arr1
         })
      }
      console.log(this.data.DgeForm)
    }
  },
  async getdeptName(id){
    const res = await request({
      url: '/dept/showInfo/' + id,
      header:{
        'Authorization':'Bearer '+wx.getStorageSync('token'),
      },
      method: "get",
    })
    // console.log(res)
    if (res.data.status !== 200){
      var title = res.data.msg
      wx.showToast({
        title: title,
        icon: 'none',
        duration: 2000
      })
    }else{
      var deptInfo = res.data.data
      if (deptInfo) {
          this.setData({
             deptForm: deptInfo
          })
      }
      // console.log(this.data.deptForm)
    }
  },
  getAllInfos:function(e){
    var deptId = e.currentTarget.dataset.value
    var dgeId = e.target.id
    this.setData({
      chkMin: e.target.chkmin
    })
    this.getdgeTypeName(dgeId)
    this.getdeptName(deptId)
    
    // this.setData({
    //   // selected: true,
    //   show: true
    // })
},
// 删除巡视记录
onClose1(event) {
  var chkId = event.currentTarget.dataset.chkid
  console.log(chkId)
   const { position, instance } = event.detail;
   let that = this
   switch (position) {
     case 'left':
     case 'cell':
     case 'outside':
       instance.close();
       break;
     case 'right':
       Dialog.confirm({
         message: '您确定要删除该条巡检记录吗？',
       }).then(() => {
         that.DelChk(chkId)
         instance.close();
       })
       .catch(() => {
         instance.close();
       });
   }
 },
 async DelChk (chkId){
  const res = await request({
    url: '/check/delete/' + chkId,
    header:{
      'Authorization':'Bearer '+wx.getStorageSync('token'),
    },
    method: "delete",
  })
  console.log(res)
  if (res.data.status !== 200){
    var title = res.data.msg
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    })
    if (title == '您还未登录'){
       wx.reLaunch({
        url: '/pages/index/index',
      })
    }
  }
  else{
    wx.showToast({
      title: '删除成功',
      icon: 'none',
      duration: 2000
    })
    this.getJudgeForm(true)
  }
},
  cancel(){
    // console.log(2)
  },
})




